package com.link510.aitools.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.common.base.Strings;
import com.link510.aitools.R;
import com.link510.aitools.core.helper.ValidateHelper;
import com.link510.aitools.core.listen.CWMListener;
import com.link510.aitools.core.message.MessageInfo;
import com.link510.aitools.framework.activity.BaseAcitivity;
import com.link510.aitools.services.Commands;
import com.link510.aitools.services.CustomWaitDialogUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class CommandActivity extends BaseAcitivity {

    @BindView(R.id.command_text)
    EditText commandText;

    @BindView(R.id.common_listview)
    ListView commonListview;

    private CustomWaitDialogUtils customWaitDialogUtils;

    String[] commonArray = {"110503022233", "11050301092233", "110500052233", "110502142233"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setTitle("命令透传");
        setContentView(R.layout.activity_command);
        customWaitDialogUtils = new CustomWaitDialogUtils(CommandActivity.this);
        initListView();
    }

    private void initListView() {

        ArrayAdapter<String> adapter = new ArrayAdapter<>(CommandActivity.this,
                R.layout.item_list_view,
                R.id.textView2, commonArray);
        commonListview.setAdapter(adapter);
        commonListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                try {
                    commandText.setText(commonArray[position]);
                } catch (Exception ignored) {

                }
            }
        });

    }

    @OnClick(R.id.send_button)
    public void send_Click() {

        try {

            String command = commandText.getText().toString().trim();

            if (Strings.isNullOrEmpty(command)) {
                ToastUtils.showShort("命令不能为空");
                return;
            }

            if (!ValidateHelper.getInstance().isHexStr(command)) {
                ToastUtils.showShort("命令只能为十六进制字符串");
                return;
            }


            if (Strings.isNullOrEmpty(this.deviceSN)) {
                ToastUtils.showShort("设备登录异常");
                return;
            }

            customWaitDialogUtils.show("命令下发中....");

            Commands.getInstance().send(this.deviceSN, command, new CWMListener<MessageInfo<String>>() {

                @Override
                public void onSuccess(String message, MessageInfo<String> stringMessageInfo) {
                    customWaitDialogUtils.close();
                    ToastUtils.showShort("设备命令下发成功");
                }

                @Override
                public void onError(int code, String message) {
                    customWaitDialogUtils.close();
                    ToastUtils.showShort("设备命令下发异常,message:" + message);
                }
            });

        } catch (Exception ignored) {

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        customWaitDialogUtils.dismiss();
    }
}